import modelEntity from "./modelEntity";
import ApprovalProcess from "./ApprovalProcess";
export default class Approval extends modelEntity {
  constructor(options) {
    super(options);
    this.Time = options.Time;
    this.Sponsor_Guid = options.Sponsor_Guid;

    this.Approval_Thing = options.Approval_Thing;
    this.Approval_Desc = options.Approval_Desc;
    this.Pay_Amount = options.Pay_Amount; this.Approval_Type_Guid = options.Approval_Type_Guid;
    this.Pay_Type_Guid = options.Pay_Type_Guid;
    this.UseSeal = options.UseSeal;
    this.Done = options.Done;

    this.approvalProcesses = [];
    if (options.approvalProcesses) {
      options.approvalProcesses.forEach(process => {
        this.approvalProcesses.push(new ApprovalProcess(process));
      })
    }

    this.Sponsor = options.Sponsor;
    this.SponsorName = this.Sponsor ? this.Sponsor.Name : "";

    this.inOutDef = options.inOutDef;
    this.inOutDefName = this.inOutDef ? this.inOutDef.Type : "";

    //撤销申请
    this.CancelApproval = options.CancelApproval;
    this.ApprovalRule = options.ApprovalRule;

    this.ApprovalRuleName="";


    //计算百分比
    this.percent = 0;
    this.percent = this.calPercent();

    this.userStateResult = this.userState;
  }

  calPercent(){

      let len = this.approvalProcesses.length;
      let count = 0;
      this.approvalProcesses.forEach(x => {
        switch (x.status) {
          case "success": count++; break;
          case "error": return { state: "fail", desc: "驳回" }; break;
          case "wait": break;
          default: break;
        }
      })
      return (count*100/len).toFixed(2);
  }
  /*
    审批流程的状态
    success  wait  fail
   */
  get nowState() {
    if (this.Remark) {
      //已撤销
      return { state: "cancel", desc: "已撤销" };
    }
    let len = this.approvalProcesses.length;
    let count = 0;
    this.approvalProcesses.forEach(x => {
      switch (x.status) {
        case "success": count++; break;
        case "error": return { state: "fail", desc: "驳回" }; break;
        case "wait": break;
        default: break;
      }
    })
    if (count == len) {
      if ((this.Approval_Type_Guid == '事项审批') && (!this.UseSeal)) {
        return { state: "success", desc: "审批完成" };
      } else {
        if (this.Done) {
          return { state: "success", desc: "核销完成" };
        } else {
          return { state: "nextDone", desc: "待核销" };
        }
      }
    }
    return { state: "wait", desc: `进行中(${count}/${len})` };
  }
  get state() {
    let len = this.approvalProcesses.length;
    let count = 0;
    this.approvalProcesses.forEach(x => {
      switch (x.status) {
        case "success": count++; break;
        case "error": return "fail"; break;
        case "wait": break;
        default: break;
      }
    })

    if (count == len) return "success";
    return `wait`;
  }

  get userState() {
    let find=-1;
    for (let i = 0; i < this.approvalProcesses.length;i++){
      if (this.approvalProcesses[i].Result == '同意'){
        if (this.approvalProcesses[i].Ref_Person_Guid == getApp().globalData.user.personnelInfo.GuidID) {
          return "finish";
        }else{
          
        }
      }else{
        if (this.approvalProcesses[i].Result == '驳回'){
          return "fail";
        }else{
          if (this.approvalProcesses[i].Ref_Person_Guid == getApp().globalData.user.personnelInfo.GuidID) {
            return "now";
          } else {
            return "noeYet";
          }
        }
      }
    }
    return "finish";
  }
  /*

   */
  get status() {
    let len = this.approvalProcesses.length;
    let count = 0;
    this.approvalProcesses.forEach(x => {
      switch (x.status) {
        case "success": count++; break;
        case "error": return "驳回"; break;
        case "wait": break;
        default: break;
      }
    })
    if (count == len) {
      if ((this.Approval_Type_Guid == '事项审批') && (!this.UseSeal)) {
        return "审批完成";
      } else {
        if (this.Done) {
          return "审批完成";
        } else {
          return "待核销";
        }
      }
    }
    return `进行中(${count}/${len})`;
  }
  static newInstance() {
    return new Approval({
      Time: null,
      Sponsor_Guid: null,
      Approval_Type_Guid: null,
      Approval_Thing: null,
      Approval_Desc: null,
      Pay_Amount: null,
      Pay_Type_Guid: null,
      UseSeal: false,
      Done: null,
      basePath: "审批",
      fileResult: [],
      approvalProcesses: [],
      CancelApproval: null,
      ApprovalRule: null,
      ApprovalRuleName:"",
    })
  }
  static loadList_my() {
    return new Promise((resolve, reject) => {
      getApp().globalData.api.http_get(`${getApp().globalData.baseUrl}/api/prj/approval/my?prj=${getApp().globalData.nowPrj.GuidID}`)
        .then(
          res => {
            console.log(res);
            let list = [];
            res.forEach(x => {
              list.push(new Approval(x));
            });
            resolve(list);
          }
        )
    })
  }
  static loadList_other() {
    return new Promise((resolve, reject) => {
      getApp().globalData.api.http_get(`${getApp().globalData.baseUrl}/api/prj/approval/other?prj=${getApp().globalData.nowPrj.GuidID}`)

        .then(
          res => {
            console.log(res);
            let list = [];
            res.forEach(x => {
              list.push(new Approval(x));
            });
            resolve(list);
          }
        )
    })
  }
  static loadList_all() {
    return new Promise((resolve, reject) => {
      getApp().globalData.api.http_get(`${getApp().globalData.baseUrl}/api/prj/approval/all?prj=${getApp().globalData.nowPrj.GuidID}`)
        .then(
          res => {
            console.log(res);
            let list = [];
            res.forEach(x => {
              list.push(new Approval(x));
            });
            resolve(list);
          }
        )
    })
  }
  create() {
    return new Promise((resolve, reject) => {
      getApp().globalData.api.http_post_json(`${getApp().globalData.baseUrl}/api/prj/approval?prj=${getApp().globalData.nowPrj.GuidID}`, this)
        .then(
          res => {
            let model = new Approval(res);
            resolve(model);
          }
        )
    })
  }
  modify() {
    return new Promise((resolve, reject) => {
      window.api.http_put("/api/prj/approval?prj=" + window.nowPrj.pCode, this)
        .then(
          res => {
            let model = new Approval(res);
            resolve(model);
          }
        )
    })
  }
  delete() {
    return new Promise((resolve, reject) => {
      window.api.http_delete("/api/prj/approval?prj=" + window.nowPrj.pCode, this)
        .then(
          res => {
            resolve(res);
          }
        )
    })
  }
}
